var year = prompt("请输入年份：");
var month = prompt("请输入月份：");

//转换数字
year = Number(year);
month = Number(month);

//1980年有多少天？
//365 366
function runNian(year) {
	//判断是闰年还是平年
	if(year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0)) {
		return true;
	} else {
		return false;
	}
}


//1980年全年到2018年全年的天数
var days = 0;
for(var i = 1980; i < year; i++) {
	if(runNian(i)) {
		days += 366;
	} else {
		days += 365;
	}
}

//2019 1月 - 4月全月一共有多少天
for(var k = 1; k < month; k++) {
	switch(k) {
		case 1:
			days += 31;
			break;
		case 2:
			if(runNian(year)) {
				days += 29;
			} else {
				days += 28;
			}
			break;
		case 3:
			days += 31;
			break;
		case 4:
			days += 30;
			break;
		case 5:
			days += 31;
			break;
		case 6:
			days += 30;
			break;
		case 7:
			days += 31;
			break;
		case 8:
			days += 31;
			break;
		case 9:
			days += 30;
			break;
		case 10:
			days += 31;
			break;
		case 11:
			days += 30;
			break;
		case 12:
			days += 31;
			break;
	}
}

//因为1980年的1月1号前面有2个空位所以加2
days += 2;
console.log(days);

//算出起始位置
var start = days % 7;

//选上这个标签#day
var day = document.querySelector("#day");

//上个月一共有多少天？
var prevDay = 0;
//找到上一个月多少天
switch(month - 1) {
	case 0:
		prevDay += 31;
		break;
	case 1:
		prevDay += 31;
		break;
	case 2:
		if(runNian(year)) {
			prevDay += 29;
		} else {
			prevDay += 28;
		}
		break;
	case 3:
		prevDay += 31;
		break;
	case 4:
		prevDay += 30;
		break;
	case 5:
		prevDay += 31;
		break;
	case 6:
		prevDay += 30;
		break;
	case 7:
		prevDay += 31;
		break;
	case 8:
		prevDay += 31;
		break;
	case 9:
		prevDay += 30;
		break;
	case 10:
		prevDay += 31;
		break;
	case 11:
		prevDay += 30;
		break;
	case 12:
		prevDay += 31;
		break;
}

//本月有多少天
var thisDay = 0;

switch(month) {
	case 1:
		thisDay += 31;
		break;
	case 2:
		if(runNian(year)) {
			thisDay += 29;
		} else {
			thisDay += 28;
		}
		break;
	case 3:
		thisDay += 31;
		break;
	case 4:
		thisDay += 30;
		break;
	case 5:
		thisDay += 31;
		break;
	case 6:
		thisDay += 30;
		break;
	case 7:
		thisDay += 31;
		break;
	case 8:
		thisDay += 31;
		break;
	case 9:
		thisDay += 30;
		break;
	case 10:
		thisDay += 31;
		break;
	case 11:
		thisDay += 30;
		break;
	case 12:
		thisDay += 31;
		break;
}
//上个月一共有多少天 - 算出起始位置 +1 = 本月1号之前的数
for(var j = prevDay - start + 1; j <= prevDay; j++) {
	day.innerHTML += '<span class="h">' + j + '</span>';
}

//取到今天
var d = new Date().getDate();
for(var s = 1; s <= thisDay; s++) {
	if(s==d){
		day.innerHTML+='<span class="today">'+s+'</span>';
	}else{
		day.innerHTML+='<span>'+s+'</span>';
	}
}

//总空位是42位   求末尾剩余的空位
var num = 42;

//算出还剩几个空位=42 - 算出起始位置 - 本月有多少天
var endDay = num -start -thisDay;
for(var e = 1;e<=endDay;e++){
	day.innerHTML+='<span class="h">'+e+'</span>';
}

//赋值
var nian = document.querySelector(".nian");
var yue = document.querySelector(".yue");

nian.innerHTML = year;
yue.innerHTML = month;